package animator.phantom.gui.keyframe;

/*
    Copyright Janne Liljeblad 2006,2007,2008

    This file is part of Phantom2D.

    Phantom2D is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Phantom2D is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Phantom2D.  If not, see <http://www.gnu.org/licenses/>.
*/

import animator.phantom.controller.*;
import animator.phantom.gui.GUIResources;
import animator.phantom.renderer.*;
import animator.phantom.renderer.param.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.beans.*;
import java.text.NumberFormat;

//--- Tool buttons and keyframe properties editors for keyframe editor.
public class KFToolButtons extends JPanel implements ActionListener, PropertyChangeListener
{
	private final String[] interpolations = { "linear","bezier" };
	private final Dimension eDim = new Dimension( 260, 22 );
	
	private boolean IGNORE_VALUE_CHANGE = false;

	private JButton zoomIn = new JButton( GUIResources.getIcon(  GUIResources.scaleZoomIn ) );
	private JButton zoomOut = new JButton( GUIResources.getIcon(  GUIResources.scaleZoomOut ) );
	private JButton autoZoom = new JButton( GUIResources.getIcon( GUIResources.scaleZoomAuto) );
	private JButton addKF = new JButton( GUIResources.getIcon(  GUIResources.addKF ) );
	private JButton deleteKF = new JButton( GUIResources.getIcon(  GUIResources.deleteKF ) );
	private JComboBox leadingIP = new JComboBox( interpolations );
	private JFormattedTextField leadingTens = new JFormattedTextField( NumberFormat.getNumberInstance() );
	private JComboBox trailingIP = new JComboBox( interpolations );
	private JFormattedTextField trailingTens = new JFormattedTextField( NumberFormat.getNumberInstance() );
	private JLabel lj = new JLabel("In" );
	private JLabel tj = new JLabel("Out" );

	private KFNaviButtons naviButtons = new KFNaviButtons();
	
	public KFToolButtons()
	{
		GUIResources.prepareMediumButton( zoomIn, this, "Zoom In" );
		GUIResources.prepareMediumButton( zoomOut, this, "Zoom Out" );
		GUIResources.prepareMediumButton( autoZoom, this, "Auto Zoom" );
		GUIResources.prepareMediumButton( addKF, this, "Add Keyframe" );
		GUIResources.prepareMediumButton( deleteKF, this, "Delete Keyframe" );

		lj.setFont( GUIResources.BASIC_FONT_12 );
		tj.setFont( GUIResources.BASIC_FONT_12 );
		leadingIP.addActionListener( this );
		leadingTens.addPropertyChangeListener( "value", this );
		leadingIP.setFont( GUIResources.TOP_LEVEL_COMBO_FONT );
		trailingIP.addActionListener( this );
		trailingTens.addPropertyChangeListener( "value", this );
		trailingIP.setFont( GUIResources.TOP_LEVEL_COMBO_FONT );

		JPanel ePanel = new JPanel();
		ePanel.setLayout(new BoxLayout( ePanel, BoxLayout.X_AXIS));
		ePanel.add( lj );
		ePanel.add( Box.createRigidArea( new Dimension( 2, 0 ) ) );
		ePanel.add( leadingIP );
		ePanel.add( Box.createRigidArea( new Dimension( 2, 0 ) ) );
		ePanel.add( leadingTens );
		ePanel.add( Box.createRigidArea( new Dimension( 6, 0 ) ) );
		ePanel.add( tj );
		ePanel.add( Box.createRigidArea( new Dimension( 2, 0 ) ) );
		ePanel.add( trailingIP );
		ePanel.add( Box.createRigidArea( new Dimension( 2, 0 ) ) );
		ePanel.add( trailingTens );
		ePanel.setPreferredSize( eDim );
		ePanel.setMaximumSize( eDim );

		JPanel p = new JPanel();
		p.setLayout(new BoxLayout( p, BoxLayout.X_AXIS));
		p.add( naviButtons );
		p.add( Box.createRigidArea( new Dimension( 3, 0 ) ) );
		p.add( zoomIn );
		p.add( zoomOut );
		p.add( autoZoom );
		p.add( Box.createRigidArea( new Dimension( 3, 0 ) ) );
		p.add( addKF );
		p.add( deleteKF );
		p.add( Box.createRigidArea( new Dimension( 6, 0 ) ) );
		p.add( ePanel );

		setLayout(new BoxLayout( this, BoxLayout.Y_AXIS));
		add( p );
		add( Box.createVerticalGlue() );

		setKeyFrame( null );
	}

	public void setKeyFrame( AnimationKeyFrame kf )
	{
		IGNORE_VALUE_CHANGE = true;
		if( kf == null )
		{
			leadingIP.setEnabled( false );
			leadingTens.setEnabled( false );
			trailingIP.setEnabled( false );
			trailingTens.setEnabled( false );
			lj.setEnabled( false );
			tj.setEnabled( false );
			leadingTens.setValue( 0.0f  );
			trailingTens.setValue( 0.0f );
		}
		else
		{
			leadingIP.setEnabled( true );
				//System.out.println(kf.getLeadingInterpolation();
			if( kf.getLeadingInterpolation() == AnimationKeyFrame.BEZIER )
			{
				leadingTens.setEnabled( true );
				leadingTens.setValue( kf.getLeadingTension()  );
			}
			else
			{
				leadingTens.setValue( 0.0f  );
				leadingTens.setEnabled( false );
			}

			trailingIP.setEnabled( true );
			if( kf.getTrailingInterpolation() == AnimationKeyFrame.BEZIER )
			{
				trailingTens.setEnabled( true );
				trailingTens.setValue( kf.getTrailingTension() );
			}
			else
			{
				trailingTens.setValue( 0.0f );
				trailingTens.setEnabled( false );
			}
	
			lj.setEnabled( true );
			tj.setEnabled( true );
			leadingIP.setSelectedIndex( kf.getLeadingInterpolation() - 1 );
			trailingIP.setSelectedIndex( kf.getTrailingInterpolation() - 1 );
		}
		IGNORE_VALUE_CHANGE = false;
	}

	public void actionPerformed( ActionEvent e )
	{
		requestFocusInWindow();

		if( e.getSource() == autoZoom )
			EditorsController.autoZoomKeyFrameEditor();
		if( e.getSource() == zoomIn )
			EditorsController.zoomInKeyFrameEditor();
		if( e.getSource() == zoomOut )
			EditorsController.zoomOutKeyFrameEditor();

		if( e.getSource() == deleteKF )
		{
			EditorsController.deleteKeyFrame();
		}
		if( e.getSource() == addKF )
		{
			EditorsController.addKeyFrame();
		}
		if( IGNORE_VALUE_CHANGE ) return;
		if( e.getSource() == leadingIP )
		{
			EditorsController.getCurrentKeyFrame().setLeadingInterpolation( leadingIP.getSelectedIndex() + 1 );
			setKeyFrame( EditorsController.getCurrentKeyFrame() );
		}
		if( e.getSource() == trailingIP )
		{
			EditorsController.getCurrentKeyFrame().setTrailingInterpolation( trailingIP.getSelectedIndex() + 1 );
			setKeyFrame( EditorsController.getCurrentKeyFrame() );
		}

		//--- repaint
		EditorsController.updateKFForValueChange();
	}

	public void propertyChange( PropertyChangeEvent e )
	{
		requestFocusInWindow();

		if( IGNORE_VALUE_CHANGE ) return;

		if( e.getSource() == leadingTens )
		{
			float newValue = ( (Number) leadingTens.getValue()).floatValue();
			EditorsController.getCurrentKeyFrame().setLeadingTension( newValue );
		}
		if( e.getSource() == trailingTens )
		{
			float newValue = ( (Number) trailingTens.getValue()).floatValue();
			EditorsController.getCurrentKeyFrame().setTrailingTension( newValue );
		}
		//--- repaint
		EditorsController.updateKFForValueChange();
	}

}//end class